const util = require('../../utils/util.js');
const api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inviterList: [],
    postersDialogShow: false,
    base64: '',
    Qrcode: '',
    loading: 0,
    allPage: 1,
    page: 1,
    size: 8,
    count: 0,
    showTips: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    this.getQrCode();
    wx.showLoading({
      title: '页面加载中...',
    });
    setTimeout(function () {
      wx.hideLoading();
      that.setData({
        loading: 1
      })
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      inviterList: [],
      allPage: 1,
      page: 1,
      size: 8,
      count: 0,
      showTips: 0,
    })
    this.getHelp();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  onClickHide() {
    this.setData({
      postersDialogShow: false
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    let name = "什么？免费领一盒凤梨酥！快点！";
    let image = "http://qiniu.hiima.cn/8636ee590090b1f7b1db1f82ed71768a82e9a8cd1603448634";
    let userID = wx.getStorageSync('userID');
    return {
      title: name,
      path: '/pages/index/index?uid=' + userID,
      imageUrl: image
    }
  },
  // 分享海报
  sharePosters() {
    let that = this;
    that.setData({
      postersDialogShow: false,
    });
    wx.showLoading({
      title: '海报生成中...',
    });
    setTimeout(function () {
      that.createNewImg();
      that.setData({
        postersDialogShow: true
      });
    }, 1000);
  },
  createNewImg() {
    var that = this;
    var context = wx.createCanvasContext('mycanvas');
    context.setFillStyle("#fff")
    context.fillRect(0, 0, 250, 500);
    var path = "/images/icon/posters-bg.jpg";
    context.drawImage(path, 0, 0, 250, 400);
    //绘制左下角文字
    context.setFontSize(14);
    context.setFillStyle('#333');
    context.setTextAlign('left');
    context.fillText("长按识别小程序", 10, 360);
    context.stroke();
    context.setFontSize(14);
    context.setFillStyle('#333');
    context.setTextAlign('left');
    context.fillText("领取优惠券和凤情梨酥", 15, 390);
    context.stroke();
    //绘制右下角小程序二维码
    var path5 = that.data.Qrcode;
    context.drawImage(path5, 155, 390, 80, 80);
    context.draw();
    //将生成好的图片保存到本地
    setTimeout(function () {
      wx.canvasToTempFilePath({
        canvasId: 'mycanvas',
        success: function (res) {
          var tempFilePath = res.tempFilePath;
          wx.hideLoading();
          that.setData({
            imagePath: tempFilePath,
            canvasHidden: true
          });
        }
      });
    }, 200);
  },
  //点击保存到相册
  savePosters: function () {
    var that = this
    wx.saveImageToPhotosAlbum({
      filePath: that.data.imagePath,
      success(res) {
        wx.showToast({
          title: '保存成功'
        });
        that.setData({
          postersDialogShow: false,
        });
      }
    })
  },
  getQrCode() {
    let that = this;
    let userID = wx.getStorageSync('userID')
    util.requestApi(api.getQrCode, {
      userID: userID
    }).then(function (res) {
      if (res.code === 200) {
        that.setData({
          base64: res.content
        });
        that.setBase();
      }
    });
  },
  // base64图片本地保存
  setBase() {
    //声明文件系统
    const fs = wx.getFileSystemManager();
    // 拿到base64
    let base = this.data.base64;
    //随机定义路径名称
    var codeimg = wx.env.USER_DATA_PATH + '/qrcode.png';
    //将base64图片写入
    fs.writeFile({
      filePath: codeimg,
      data: base.slice(22),
      encoding: 'base64',
      success: (res) => {
        this.setData({
          Qrcode: codeimg
        });
      }
    });
  },
  // 获取优惠券列表
  getHelp() {
    let that = this;
    let userID = wx.getStorageSync('userID')
    util.requestApi(api.getHelp, {
      userID: userID,
      page: that.data.page,
      size: that.data.size
    }).then(function (res) {
      if (res.code === 200) {
        that.setData({
          inviterList: that.data.inviterList.concat(res.content.data),
          count: res.content.count,
          allPage: Math.ceil(res.content.count / that.data.size),
          showTips: res.content.data.length < that.data.size ? 1 : 0,
        });
      }
    });
  },
  getMoreHelpList() {
    let that = this;
    if (that.data.page >= that.data.allPage) {
      that.setData({
        showTips: 1
      });
      return false;
    }
    that.setData({
      page: that.data.page + 1
    });
    that.getHelp();
  }
})